package com.zc.webnews.utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.zc.webnews.application.Constant;
import com.zc.webnews.application.MyApplication;
import com.zc.webnews.bean.NewsDetailsBean;
import com.zc.webnews.model.Account;
import com.zc.webnews.ui.LoginActivity;
import com.zc.webnews.ui.NewsActivity;
import com.zc.webnews.ui.RegisterActivity;
import com.zc.webnews.ui.ShowCollectionActivity;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * Created by Administrator on 2016/11/20.
 */
public class MyBmobUtils {

    public static boolean identifySuccess = false;
    public static boolean registerSuccess = true;

    public Context context;
    public void queryUserHead(){
        BmobQuery<Account> query = new BmobQuery<>();
        query.findObjects(new FindListener<Account>() {
            @Override
            public void done(List<Account> list, BmobException e) {
                for (Account a :
                        list) {
                    if (a.getAccount().equals(MyApplication.mAccount.getAccount())){
                        MyApplication.mAccount = a;
                        ((NewsActivity)context).handler.sendEmptyMessage(200);
                        break;
                    }
                }
            }
        });
    }
    /**
     * 上传头像
     * @param image
     */
    public void addUserInfo(BmobFile image){
        MyApplication.mAccount.setImage(image);
        final String id = MyApplication.mAccount.getObjectId();
        MyApplication.mAccount.getImage().uploadblock(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (e == null){
                    MyApplication.mAccount.update(id, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                                if (e == null){
                                    Toast.makeText(context,"上传成功",Toast.LENGTH_SHORT).show();
                                }else {
                                    Log.e("更新失败",e.getMessage());
                                }
                        }
                    });
                }else {
                    Log.e("更新失败",e.getMessage());
                    //Toast.makeText(context,"上传失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
//        MyApplication.mAccount.update(id,new UpdateListener() {
//            @Override
//            public void done(BmobException e) {
//                if(e==null){
//                    Log.e("更新成功:",MyApplication.mAccount.getUpdatedAt());
//                }else{
//                    Log.e("更新失败：" , e.getMessage());
//                }
//            }
//        });
    }

    public void deleteCollection(String objectId){
        final NewsDetailsBean newsDetailsBean = new NewsDetailsBean();
        newsDetailsBean.setObjectId(objectId);
        newsDetailsBean.delete(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e==null){
                    Log.e("删除成功:","ok");
                }else{
                    Log.e("删除失败","failed");
                }
            }
        });
    }

    /**
     * 从bmob获取收藏的文章,以后的修改方案，加入本地数据库中，速度更快
     */
    public void queryCollection(){
        BmobQuery<NewsDetailsBean> query = new BmobQuery<>();
        query.findObjects(new FindListener<NewsDetailsBean>() {
            @Override
            public void done(List<NewsDetailsBean> list, BmobException e) {
                ShowCollectionActivity.collectionList.clear();
                for (NewsDetailsBean n:
                        list) {
                    if (n.getAccount().equals(MyApplication.mAccount.getAccount())){
                        ShowCollectionActivity.collectionList.add(n);
                    }
                }
                ((ShowCollectionActivity)context).handler.sendEmptyMessage(Constant.SUCCESS);
            }
        });
    }
    /**
     * 增加收藏文章
     * @param list
     */
    public void addCollection(List<NewsDetailsBean> list){
        for (NewsDetailsBean m :
                list) {
            m.save(new SaveListener<String>() {
                @Override
                public void done(String s, BmobException e) {
                    if (e == null){
                        Log.e("addCollection",s);
                    }else {
                        Log.e("addCollection","failed");
                    }
                }
            });
        }
    }

    /**
     * 增加新用户
     * @param userPhone 账号
     * @param password 密码
     */
    public void addUser(String userPhone,String password){
        Account account = new Account();
        account.setAccount(userPhone);
        account.setPassword(password);
        account.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null){
                    Log.e("Bmob-addUserSuccess",s);
                    ((LoginActivity)context).handler.sendEmptyMessage(2);
                }else {
                    Log.e("Bmob-addUserFailed","Failed");
                }
            }
        });
    }

    /**
     * 登录查询是否账号密码输入错误
     * @param userPhone 账号
     * @param password  密码
     */
    public void queryAccount(final String userPhone, final String password){
        BmobQuery<Account> bmobQuery = new BmobQuery<>();
        bmobQuery.findObjects(new FindListener<Account>() {
            @Override
            public void done(List<Account> list, BmobException e) {
                for (Account a :
                        list) {
                    if (a.getAccount().equals(userPhone)&&a.getPassword().equals(password)){
                        identifySuccess = true;
                        break;
                    }
                }
                (( LoginActivity)context).handler.sendEmptyMessage(0);
            }
        });
    }

    /**
     * 注册新用户验证
     * @param userPhone 账号
     */
    public void registerChecked(final String userPhone){
        BmobQuery<Account> query = new BmobQuery<>();
        query.findObjects(new FindListener<Account>() {
            @Override
            public void done(List<Account> list, BmobException e) {
                for (Account a :
                        list) {
                    if (a.getAccount().equals(userPhone)){
                        registerSuccess = false;
                        break;
                    }
                }
                if (!registerSuccess){
                    ((RegisterActivity)context).handler.sendEmptyMessage(404);
                }else {
                    ((RegisterActivity)context).handler.sendEmptyMessage(200);
                }
            }
        });
    }
}
